/*
 *   power by    weishengjian
 *   datetime    2018/7/14 10:26
 */

/*
 *  将对象转变为class字符串
 */
export function classes (obj) {
  let classesStr = ''
  for (let className in obj) {
    if (obj.hasOwnProperty(className) && !!obj[className]) classesStr += className
  }
  return classesStr
}

/*
 * 将对象转变为style字符串，对象属性的camel命名会转换为中划线命名
 */
export function styles (obj) {
  let stylesStr = ''
  for (let styleName in obj) {
    if (obj.hasOwnProperty(styleName)) stylesStr += `${styleName.replace(/([A-Z])/g, '-$1').toLowerCase()}:${px2rpx(obj[styleName])};`
  }
  return stylesStr
}

let windowWidth
(async () => {
  let systemInfo = await wx.getSystemInfoSync()
  windowWidth = systemInfo.windowWidth
}).call(this)

export function px2rpx (str) {
  if (/rpx/.test(str)) {
    return str
  } else if (/px/.test(str)) {
    let val = str.substring(0, str.indexOf('px'))
    /* 在代码中硬编码时，所有px单位都是以375px为整个屏幕宽度作为参照，所以这里需要转换一下 */
    val = val * windowWidth / 375
    return `${(val - 0) * 750 / windowWidth}rpx`
  } else return str
}
